package breeze.tool.life.activities;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import breeze.tool.life.R;
import brz.breeze.app_utils.BActivity;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import breeze.tool.life.App;
import android.view.Gravity;
import android.widget.TextView;
import android.graphics.Color;
import android.widget.ProgressBar;
import android.app.AlertDialog;
import android.widget.EditText;
import breeze.tool.life.utils.AppStyles;
import android.content.DialogInterface;
import breeze.tool.life.database.AppSqlUtils;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import android.widget.SimpleExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView;
import android.widget.Adapter;
import android.view.View.OnLongClickListener;
import breeze.tool.life.utils.ExpressInspectionApi;
import brz.breeze.app_utils.BAppUtils;
import android.widget.AdapterView.OnItemClickListener;

public class ExpressInspection extends BActivity {
	
	
	private FrameLayout mFramelayout;
	
	private SimpleExpandableListAdapter mAdapter;
	private ExpandableListView mListView;
	
	private List<Map<String,?>> mGroups = new ArrayList<>();
	private List<List<Map<String,?>>> mChilds = new ArrayList<>();

	@Override
	public void init() {
		//状态栏沉浸
		getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
		if(getActionBar()!=null){
			getActionBar().hide();
		}
		if(Build.VERSION.SDK_INT>=21){
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		}
		//获取控件
		mFramelayout = find(R.id.activityexpressinspectionFrameLayout1);
		mListView = new ExpandableListView(this);
		mListView.setVerticalScrollBarEnabled(false);
	}

	@Override
	public void initData() {
		initDatas();
	}
	
	private void initDatas(){
		mGroups.clear();
		mChilds.clear();
		turnToLoading();
		final List<String> datas = AppSqlUtils.getAllDatas();
		if(datas!=null){
			BAppUtils.execute(new Runnable(){
					@Override
					public void run() {
						for(String group:datas){
							Map<String,String> map = new HashMap<>();
							map.put("Name",group);
							mGroups.add(map);
							List<Map<String,?>> child_datas = new ArrayList<>();

							List<String> datas2 = ExpressInspectionApi.getContent(group);

							if(datas2!=null){
								for(String str:datas2){
									Map<String,String> map2 = new HashMap<>();
									map2.put("Name",str);
									child_datas.add(map2);
								}
							}
							
							mChilds.add(child_datas);
						}
						runOnUiThread(new Runnable(){
							public void run(){
								mAdapter = new SimpleExpandableListAdapter(ExpressInspection.this,
																		   mGroups,android.R.layout.simple_expandable_list_item_1,new String[]{"Name"},new int[]{android.R.id.text1},
																		   mChilds,android.R.layout.simple_expandable_list_item_2,new String[]{"Name"},new int[]{android.R.id.text2});
								mListView.setAdapter(mAdapter);
								mListView.setOnItemLongClickListener(new OnItemLongClickListener(){
										@Override
										public boolean onItemLongClick(AdapterView<?> p1, View p2, int p3, long p4) {
											long packedposition = mListView.getExpandableListPosition(p3);
											long group = mListView.getPackedPositionGroup(packedposition);
											long child = mListView.getPackedPositionChild(packedposition);
											if(child==-1){
												//长按组
												Map map = mGroups.get((int)group);
												final String content = (String) map.get("Name");
												new AlertDialog.Builder(ExpressInspection.this)
												.setTitle("提示")
												.setMessage("是否删除:"+content+"的快递信息？")
													.setPositiveButton("是", new DialogInterface.OnClickListener(){
														@Override
														public void onClick(DialogInterface p1, int p2) {
															toast("删除成功");
															AppSqlUtils.delete(content);
															initDatas();
														}
													})
													.setNegativeButton("否",null)
													.create().show();
											}else{
												Map map = mChilds.get((int)group).get((int)child);
												String content = (String) map.get("Name");
												new AlertDialog.Builder(ExpressInspection.this)
												.setTitle("物流信息")
												.setMessage(content)
												.setPositiveButton("确定",null)
												.create().show();
											}
											return false;
										}
									});
								turnToList();
							}
						});
					}
			});
		}else{
			turnToNothing();
		}
	}
	
    /*
	*@author BREEZE
	*@date 2021-11-20 18:18:15
    */

    public static final String TAG = "ExpressInspection";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_express_inspection);
		init();
		initData();
    }
	
	private void turnToNothing(){
		runOnUiThread(new Runnable(){
				@Override
				public void run() {
					mFramelayout.removeAllViews();
					LinearLayout linear = new LinearLayout(App.getContext());
					linear.setGravity(Gravity.CENTER);
					TextView textview = new TextView(App.getContext());
					textview.setText("什么都没有噢");
					textview.setTextColor(Color.BLACK);
					linear.addView(textview);
					mFramelayout.addView(linear);
				}
			});
	}
	
	private void turnToLoading(){
		runOnUiThread(new Runnable(){
				@Override
				public void run() {
					mFramelayout.removeAllViews();
					LinearLayout linear = new LinearLayout(App.getContext());
					linear.setOrientation(LinearLayout.VERTICAL);
					linear.setGravity(Gravity.CENTER);
					ProgressBar pb = new ProgressBar(App.getContext());
					pb.setProgressDrawable(getResources().getDrawable(android.R.drawable.progress_indeterminate_horizontal));
					linear.addView(pb);
					mFramelayout.addView(linear);
				}
			});
	}
	
	private void turnToList(){
		runOnUiThread(new Runnable(){
			public void run(){
				mFramelayout.removeAllViews();
				mFramelayout.addView(mListView);
			}
		});
	}
	
	public void addExpress(View v){
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("添加快递");
		LinearLayout line = new LinearLayout(this);
		final EditText edittext = new EditText(this);
		edittext.setBackground(AppStyles.getEdittextStyle());
		LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
		ll.setMargins(70,40,70,40);
		edittext.setHint("请输入快递单号");
		edittext.setTextSize(15);
		edittext.setLayoutParams(ll);
		line.addView(edittext);
		alert.setView(line);
		alert.create().setCanceledOnTouchOutside(true);
		alert.setPositiveButton("确定", new DialogInterface.OnClickListener(){
				@Override
				public void onClick(DialogInterface p1, int p2) {
					String content = edittext.getText().toString();
					if(content!=null&&!content.isEmpty()){
						AppSqlUtils.addData(content);
						initDatas();
					}else{
						toast("快递单号输入有误");
					}
				}
		});
		alert.setNegativeButton("取消",null);
		alert.show();
	}
    
}
